<div class="layuimini-container layuimini-page-anim" divname="admin">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" maxlength="30" name="UserName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" maxlength="30" name="Phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
            
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="AdminPageTable" lay-filter="AdminPageTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table', 'miniPage', 'element', 'faceAPI'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var faceAPI = layui.faceAPI;

        table.render({
            elem: '#AdminPageTable',
            //**************************异步请求开始***********************
            url: 'api/User/GetUserTable',
            method: 'post',
            headers: {
                //token: 'sasasas',
                Authorization: "Bearer " + faceAPI.GetJWTToken()
            },//增加Token验证头
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                if (res.errCode == 0) {
                    var data = res.content;
                    var ret = {
                        "code": res.errCode, //解析接口状态
                        "msg": res.error, //解析提示文本
                        "count": data.totalCount, //解析数据长度
                        "data": data.dataList //解析数据列表
                    };
                    return ret;

                } else{
                    /*layer.open({
                        title: '请求错误',
                        content: '(' + res.errCode + ')' + res.error
                    });  */ 

                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": 0, //解析数据长度
                        "data":[] //解析数据列表
                    };
                }
                
            },
            //**************************异步请求结束***********************

            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'userID', width: 80, title: 'ID', sort: true },
                { field: 'userName', width: 120, title: '用户名', sort: true },
                { field: 'role', width: 80, title: '身份', sort: true },
                { field: 'phone', width: 120, title: '电话', sort: true },
                { field: 'onlineTime', title: '最后登录时间', sort: true },
                { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = data.field;
            //执行搜索重载
            table.reload('AdminPageTable', {
                page: {
                    curr: 1
                }
                , where: result
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(AdminPageTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/admin/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                faceAPI.AdminAddPageResult = null;
                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {
                        
                        var rst = faceAPI.AdminAddPageResult;
                        if (rst != null && rst.result) {
                            //执行搜索重载
                            table.reload('AdminPageTable', {
                                page: {
                                    curr: 1
                                }
                            }, 'data');
                        };
                        
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('AdminPageTable')
                    , data = checkStatus.data;
                var userids = [];
                //console.log(data);
                data.forEach(function (item, index) {
                    if (item.userID != 1) {
                        userids.push(item.userID);
                    }
                });
                if (userids.length == 0) {
                    layer.alert('请勾选需要删除的用户ID');
                    return;
                }
                faceAPI.Ajax({
                    type: 'Post',
                    data: { userids },
                    url: '/api/User/Delete',
                    success: function (result) {
                        if (result.result == false) {
                            layer.alert(result.error, { icon: 2 });
                        }
                        else {
                            //刷新页面
                            table.reload('AdminPageTable', {
                                page: {
                                    curr: 1
                                }
                            }, 'data');
                        }

                    }
                });
                //layer.alert(JSON.stringify(userids));
            }
        });


        table.on('tool(AdminPageTableFilter)', function (obj) {
            var data = obj.data;

            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/admin/edit.html');
                var openWH = miniPage.getOpenWidthHeight();
                faceAPI.AdminEditPage = data;//向layer页面传值，传值主要代码
                faceAPI.AdminEditPageResult=null;
                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {
                        
                        var rst = faceAPI.AdminEditPageResult;
                        console.log(rst);
                        if (rst != null && rst.result) {
                            var newuser = rst.content;
                            //执行搜索重载
                            obj.update({
                                phone: newuser.phone,
                                role: newuser.Role
                            });
                        };

                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
            else if (obj.event === 'delete') {
                if (data.userID == 1) {
                    layer.open({ content: '管理员不可以删除' });
                    return;
                };

                layer.confirm('操作确认：删除管理员[' + data.userName + ']?', function (index) {
                    var userids = [];
                    userids.push(data.userID);
                    faceAPI.Ajax({
                        type: 'Post',
                        data: { userids },
                        url: '/api/User/Delete',
                        success: function (result) {
                            if (result.result == false) {
                                layer.alert(result.error, { icon: 2 });
                            }
                            else {
                                //刷新页面
                                obj.del();
                            }

                        }
                    });
                    
                   
                    layer.close(index);
                });
            }
        });
    });



</script>